package com.qidianit.mes.md.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.common.pojo.MESResult;
import com.qidianit.common.util.Constant;
import com.qidianit.mes.md.mapper.MaterialMapper;
import com.qidianit.mes.md.service.MaterialService;
import com.qidianit.mes.pojo.Material;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author djl
 * @Time 2024/10/14 20:28
 */
@Service
public class MaterialServiceImpl implements MaterialService {
    @Autowired
    private MaterialMapper materialMapper;
    @Override
    public MESResult selectByPage(Material material, Integer page, Integer limit) {
        //开启分页  参数1： 页数  参数2： 每页大小
        PageHelper.startPage(page,limit);
        //查询全部
        List<Material> materials = materialMapper.selectByPage(material);
        //创建一个PageInfo对象，构造方法中传入查询的结果集合。
        PageInfo<Material> pageInfo = new PageInfo<>(materials);
        // 将pageInfo对象设置到QdResult中
        return MESResult.success(pageInfo);// 返回结果
    }

    @Override
    public MESResult save(Material material) {
        if (material.getMaterialCode() == null || material.getMaterialCode() == "") {
            return MESResult.error("物料编码不能为空");
        }
        if (material.getMaterialName() == null || material.getMaterialName() == "") {
            return MESResult.error("物料名称不能为空");
        }
        if (material.getMaterialUnitCode() == null || material.getMaterialUnitCode() == "") {
            return MESResult.error("单位必须选其一");
        }
        if (material.getKindId() == null) {
            return MESResult.error("物料/产品分类必须选其一");
        }
        material.setCreateTime(new Date());
        material.setModifyTime(new Date());
        return materialMapper.save(material) == 1 ? MESResult.success(material):MESResult.error();
    }

    @Override
    public MESResult updateMaterial(Material material) {
        if (material.getMaterialCode() == null || material.getMaterialCode() == "") {
            return MESResult.error("物料编码不能为空");
        }
        if (material.getMaterialName() == null || material.getMaterialName() == "") {
            return MESResult.error("物料名称不能为空");
        }
        if (material.getSafeFlag() == 2) {
            material.setMaxStock(null);
            material.setMinStock(null);
        }
        return materialMapper.updateById(material) == 1?MESResult.success():MESResult.error();
    }

    @Override
    public MESResult selectById(Long materialId) {
        return MESResult.success(materialMapper.selectById(materialId));
    }

    @Override
    public MESResult delete(Long materialId) {
        // 修改状态
        return MESResult.isSuccess(materialMapper.deleteById(materialId));
    }

    @Override
    public MESResult deletes(String[] ids) {
        return MESResult.success(Constant.RESPONSE_STATUS_OK,"OK",materialMapper.updateMaterialsByIds(ids,Constant.DATA_STATUS_DELETE));
    }

    @Override
    public MESResult getAllMaterial() {
        return MESResult.success(materialMapper.getAllMaterial());
    }
}
